﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;


/// <summary>
/// Summary description for ViewControlBase
/// </summary>
public abstract class ViewControlBase<T>:UserControl,IView<T>
{
    private T model;
    public T Model
    {
        set{ model = value;}
        get { return model;}
 
    }

    #region IView<T> Members

    public void UpdateView(T o)
    {
        foreach (PropertyInfo property in o.GetType().GetProperties())
        {
            object value = property.GetValue(o, null);

            
                if (value is string)
                {
                    TextBox textBox = ((TextBox)this.FindControl("tb_" + property.Name));
                    if(textBox!=null)
                    {
                            textBox.Text = value.ToString();
                    }

                }
                else if (value is bool)
                {
                    CheckBox checkBox = ((CheckBox)this.FindControl("cb_" + property.Name));
                    if(checkBox!=null)
                    {
                        checkBox.Checked = (bool)value ;
                    }
                }
                else if (value is int)
                {
                    TextBox textBox = ((TextBox)this.FindControl("tb_" + property.Name));
                    if (textBox != null)
                    {
                        textBox.Text = value.ToString();
                    }
                    DropDownList dropDownList = ((DropDownList)this.FindControl("ddl_" + property.Name));
                    if (dropDownList != null)
                    {
                        dropDownList.SelectedValue = value.ToString();
                    }
                }
          
           
        }

    }


    public T UpdateModel(T o)
    {
        foreach (PropertyInfo property in o.GetType().GetProperties())
        {
            Type value = property.PropertyType;


            if (value== typeof(string))
            {
                TextBox textBox = ((TextBox)this.FindControl("tb_" + property.Name));
                if (textBox != null)
                {
                    //textBox.Text = value.ToString();
                    property.SetValue(o, textBox.Text, null);
                }

            }
            else if (value == typeof(bool))
            {
                CheckBox checkBox = ((CheckBox)this.FindControl("cb_" + property.Name));
                if (checkBox != null)
                {
                    //checkBox.Checked = (bool)value;
                    property.SetValue(o, checkBox.Checked, null);
                }
            }
            else if (value == typeof(int))
            {
                TextBox textBox = ((TextBox)this.FindControl("tb_" + property.Name));
                if (textBox != null)
                {
                    
                    property.SetValue(o, int.Parse(textBox.Text), null);
                }
                DropDownList dropDownList = ((DropDownList)this.FindControl("ddl_" + property.Name));
                if (dropDownList != null)
                {

                    property.SetValue(o, int.Parse(dropDownList.SelectedValue), null);
                }
            }


        }

        return o;
    }
 
    #endregion

   
}
